package com.mz.mattress.admin.api.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.MemberCardReq;
import com.mz.mattress.admin.api.resp.MerchantCardSum;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.MemberCardService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.MemberCard;
import com.mz.mattress.api.jwt.MerchantJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "memberCard")
public class MemberCardController {

    @Autowired
    private MemberCardService memberCardService;

    @GetMapping("/listPage")
    public Result<IPage<MemberCard>> listPage(MemberCardReq req) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        log.info("merchantId={}", userJwt.getTypeId());
        req.setMerchantId(userJwt.getTypeId());
        return memberCardService.listPage(req);
    }

    @GetMapping("/{id}")
    public Result<MemberCard> detail(@PathVariable Integer id) {
        return memberCardService.getById(id);
    }

    @GetMapping("/selectSum")
    public Result<MerchantCardSum> selectSum(@RequestParam Integer cardId) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        log.info("merchantId={}", userJwt.getTypeId());
        return memberCardService.selectSum(userJwt.getTypeId(), cardId);
    }

}
